package com.ctsi.ssdc.service.impl;

import com.ctsi.ssdc.entity.EconomicIndicators;
import com.ctsi.ssdc.mapper.EconomicIndicatorsMapper;
import com.ctsi.ssdc.service.EconomicIndicatorsService;
import io.swagger.models.auth.In;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.util.*;
import java.util.concurrent.Future;
import javax.annotation.Resource;

/**
 * 用户信息表日期(EconomicIndicators)表服务实现类
 *
 * @author makejava
 * @since 2023-03-11 20:57:33
 */
@Service("economicIndicatorsService")
public class EconomicIndicatorsServiceImpl implements EconomicIndicatorsService {
    @Resource
    private EconomicIndicatorsMapper economicIndicatorsMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public EconomicIndicators queryById(Long id) {
        return this.economicIndicatorsMapper.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param economicIndicators 筛选条件
     * @param pageRequest        分页对象
     * @return 查询结果
     */
    @Override
    public Page<EconomicIndicators> queryByPage(EconomicIndicators economicIndicators, PageRequest pageRequest) {
        long total = this.economicIndicatorsMapper.count(economicIndicators);
        return new PageImpl<>(this.economicIndicatorsMapper.queryAllByLimit(economicIndicators, pageRequest), pageRequest, total);
    }

    /**
     * 分页查询(根据excel 排序)
     *
     * @param economicIndicators 筛选条件
     * @param pageRequest        分页对象
     * @return 查询结果
     */
    @Override
    public Page<EconomicIndicators> queryByPageOrder(EconomicIndicators economicIndicators, PageRequest pageRequest) {
        long total = this.economicIndicatorsMapper.count(economicIndicators);
        List<EconomicIndicators> list = this.economicIndicatorsMapper.queryAllByLimit(economicIndicators, pageRequest);
        Map<String, Integer> orderRules = orderRules();
        Map<Integer, EconomicIndicators> map = new TreeMap<>();
        // 将数据存放至排序map
        list.forEach(e -> {
            Integer order = orderRules.get(e.getIndicators());
            if (order != null) {
                map.put(order, e);
            } else {
                map.put(99, e);
            }
        });
        // map to list
        List<EconomicIndicators> resList = new ArrayList<>();
        list = new ArrayList<>();
        map.forEach((k, v) -> {
            resList.add(v);
        });
        return new PageImpl<>(resList, pageRequest, total);
    }

    private Map<String, Integer> orderRules() {
        Map<String, Integer> orderMap = new HashMap<>();
        orderMap.put("地区生产总值",  1);
        orderMap.put("一般公共预算收入",  2);
        orderMap.put("#税收占比",  3);
        orderMap.put("全社会固定资产投资",  4);
        orderMap.put("#工业投资",  5);
        orderMap.put("规上工业总产值",  6);
        orderMap.put("社会消费品零售总额",  7);
        orderMap.put("进出口总额",  8);
        orderMap.put("#出口总额",  9);
        orderMap.put("实际使用外资",  10);
        return orderMap;
    }

    /**
     * 新增数据
     *
     * @param economicIndicators 实例对象
     * @return 实例对象
     */
    @Override
    public EconomicIndicators insert(EconomicIndicators economicIndicators) {
        this.economicIndicatorsMapper.insert(economicIndicators);
        return economicIndicators;
    }

    /**
     * 修改数据
     *
     * @param economicIndicators 实例对象
     * @return 实例对象
     */
    @Override
    public EconomicIndicators update(EconomicIndicators economicIndicators) {
        this.economicIndicatorsMapper.update(economicIndicators);
        return this.queryById(economicIndicators.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.economicIndicatorsMapper.deleteById(id) > 0;
    }

    /**
     * 批量添加数据
     *
     * @param list
     * @return 是否成功
     */
    @Override
    public boolean insertBatch(List<EconomicIndicators> list) {
        int insertBatch = this.economicIndicatorsMapper.insertBatch(list);
        return insertBatch > 0;
    }

    /**
     * 根据日期删除
     *
     * @param date
     * @return 是否成功
     */
    @Override
    public boolean removeByDate(Date date) {
        return economicIndicatorsMapper.removeByDate(date);
    }

    @Override
    public EconomicIndicators queryByIndicator(Date date, String indicators) {
        return economicIndicatorsMapper.queryByIndicator(date, indicators);
    }

    @Override
    public Date getLatestDataTime() {
        return economicIndicatorsMapper.getLatestDataTime();
    }
}
